package com.nexus.plugin.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.nexus.core.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 基础字段填充
 * @Date 2025/7/25 15:34
 * @Author luzhengning
 **/
@Slf4j
@Component
public class MyBatisMetaObjectHandler implements MetaObjectHandler {

    /**
     * 添加数据时填充
     * @author luzhengning
     * @date 2023/3/22 11:37
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
        this.strictInsertFill(metaObject, "createBy", String.class, getCurrentUserId());
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());

        //自定义字段填充
        Class<?> aClass = metaObject.getOriginalObject().getClass();
        for (Field field : aClass.getDeclaredFields()) {
            // MetaObjectFill annotation = field.getAnnotation(MetaObjectFill.class);
            // if(annotation==null){
            //     continue;
            // }
            //组织机构字段填充
            // if(annotation.metaType()== FillMetaType.OrgId){
            //     String value=getCurrentUserOrgId();
            //     this.strictInsertFill(metaObject, field.getName(), String.class, value);
            // }
            // //登录用户名称
            // if(annotation.metaType()== FillMetaType.UserName){
            //     String value=getCurrentUserName();
            //     this.strictInsertFill(metaObject, field.getName(), String.class, value);
            // }

        }

    }

    /**
     * 修改时填充
     * @author luzhengning
     * @date 2023/3/22 11:42
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        String currentUserId = getCurrentUserId();
        if(ObjectUtils.isNotEmpty(currentUserId)){
            this.strictUpdateFill(metaObject, "updateBy", String.class, currentUserId);
        }
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());

        Class<?> aClass1 = metaObject.getOriginalObject().getClass();
        for (Field field :aClass1.getDeclaredFields()) {
            if("org".equals(field.getName())){
                this.strictUpdateFill(metaObject, "org", String.class, null);
            }
        }



        //自定义字段填充
        // Class<?> aClass = metaObject.getOriginalObject().getClass();
        // for (Field field : aClass.getDeclaredFields()) {
        //     MetaObjectFill annotation = field.getAnnotation(MetaObjectFill.class);
        //     if(annotation==null){
        //         continue;
        //     }
        //     //组织机构字段填充
        //     if(annotation.metaType()== FillMetaType.OrgId){
        //         String value=getCurrentUserOrgId();
        //         this.strictInsertFill(metaObject, field.getName(), String.class, value);
        //     }
        //
        //     //登录用户名填充
        //     if(annotation.metaType()== FillMetaType.UserName){
        //         String value=getCurrentUserName();
        //         this.strictInsertFill(metaObject, field.getName(), String.class, value);
        //     }
        // }
    }
    /**
     * 获取当前登录用的ID
     * @author luzhengning
     * @date 2023/4/6 11:07
     */
    private String getCurrentUserId(){
        try {
            return "";
        } catch (Exception e){
            return null;
        }
    }

    /**
     * 获取当前用户组织机构ID
     * @Date 2023/6/8 16:34
     * @Author luzhengning
     **/
    private String getCurrentUserOrgId(){
        try {
            return "";
        } catch (Exception e){
            return null;
        }
    }

}
